package com.service.impl;

import com.common.exception.db.InsertException;
import com.common.exception.db.UpdateException;
import com.dao.TranslationDao;
import com.entity.PromotionUserInfo;
import com.service.TranslationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TranslationServiceImpl implements TranslationService {
    @Autowired
    private TranslationDao dao;


    @Override
    @Transactional
    public int findTranslation(Integer userId, Integer pid) {
        // 1.先查询此用户是否误操作  以前已经存在了数据,就直接返回 没有受邀权
        int count = dao.findUser(userId);
        if(count > 0){  //大于0为误操作 直接返回
            return 1;
        }else{
        // 2.先给此用户赋值pid
           count = dao.insertPid(userId,pid);
           if(count > 0){ //大于0为新增Pid成功
               int type;
       //3.查询此pid为老师还是家长   如果是家长type为1  老师type为2
               String types = dao.findPidUserType(pid);
               if(types.equals("student")){
                   type = 1;
               }else{
                   type = 2;
               }
        // 4.给推广表插入一条数据
            count = dao.insertPromotionUser(userId,pid,type);
            if(count > 0){
        // 5. 如果type 为 1 给pid加钱
              if(type == 1){
                        Date date = new Date();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date);
                        calendar.add(Calendar.DATE, +7);
                 count = dao.insertMoney(pid,date,calendar.getTime());
                 if(count > 0){
                     return count;
                 }else{
                     throw new UpdateException("修改钱数失败");
                 }
              }else {
                    return count;
              }
            }else{
                throw new InsertException("推广表插入失败");
            }
           }else{
               throw new InsertException("赋值Pid失败");
           }
        }
    }

    @Override
    @Transactional
    public int updatePid(Integer userId, Integer pid) {
        // 1.先查询此用户是否存在pid  以前已经存在了数据,就直接返回 没有受邀权
        int count = dao.findUserPid(userId);
        if(count != 0){  //如果pid不等于数据库存在的pid
            return 1;
        }else{
            // 2.先给此用户赋值pid
            count = dao.insertPid(userId,pid);
            if(count > 0){ //大于0为新增Pid成功
                count = dao.insertPromotionUser(userId,pid,1);
                if(count > 0){
                    return count;
                }else{
                    throw new InsertException("新增推广失败");
                }
            }else{
                throw new InsertException("赋值Pid失败");
            }
        }
    }

    @Override
    public Map findDetails(Integer userId,Integer limit) {
        Map map=new HashMap();
        List<PromotionUserInfo> Promotions = dao.findDetails(userId,limit);
        int teacherCount = dao.findteacherCount (userId);
        int studentCount = dao.studentCount(userId);
        map.put("PromotionUserInfo",Promotions);
        map.put("teacherCount", teacherCount);
        map.put("studentCount", studentCount);
        return map;
    }


}
